package com.itstu.controller;

import com.itstu.annotaion.LoginUser;
import com.itstu.model.AjaxResult;
import com.itstu.pojo.Apply;
import com.itstu.pojo.User;
import com.itstu.pojo.dto.ApplyDTO;
import com.itstu.service.ApplyService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 梁卓
 * @Version 1.0
 * @ClassName ApplyController
 * @Description TODO
 * @Date 2024/10/08 21:40
 */
@RestController
@RequestMapping("/apply")
public class ApplyController {

    @Resource
    private ApplyService applyService;

    @PostMapping
    public AjaxResult addApply(@LoginUser User user, @RequestBody Apply apply) {
        apply.setUid(user.getId());
        Apply resultApply = applyService.getFriendsByUidAndTid(apply);
        if (resultApply != null) {
            return AjaxResult.success("已发出申请，等待对方同意！");
        }
        applyService.addApply(apply);
        return AjaxResult.success("添加申请成功！");
    }

    /**
     * 获取好友列表
     * @param user 登录用户信息
     * @return 好友列表
     */
    @GetMapping("/getFriends")
    public AjaxResult getFriends(@LoginUser User user) {
        List<User> resultList =  applyService.getFriendsByUid(user.getId());
        return AjaxResult.success(resultList);
    }

    /**
     * 同意或拒绝申请
     * @param user 登录用户信息
     * @param applyDTO 申请信息（status: 1 接受 -- 0 拒绝）
     * @return 操作结果
     */
    @PostMapping("/agreeOrReject")
    public AjaxResult agreeOrReject(@LoginUser User user, @RequestBody ApplyDTO applyDTO) {
        applyDTO.setUid(user.getId());
        applyService.agreeOrReject(applyDTO);
        return AjaxResult.success("操作成功！");
    }

}
